package com.webchat.admin.controller;


import com.webchat.admin.service.AccountManagementService;
import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.helper.SessionHelper;
import com.webchat.domain.vo.request.CreateAccountRequestVO;
import com.webchat.domain.vo.request.CreateRobotRequestVO;
import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin-service/account")
public class AccountManagementController {

    @Autowired
    private AccountManagementService accountManagementService;

    /**
     * 账号分页查询
     *
     * @param roleCode
     * @param userId
     * @param userName
     * @param mobile
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public APIPageResponseBean<UserBaseResponseInfoVO> page(
            @RequestParam(value = "roleCode") Integer roleCode,
            @RequestParam(value = "userId", required = false) String userId,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "mobile", required = false) String mobile,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        return accountManagementService.page(userId, roleCode, userName, mobile, pageNo, pageSize);
    }

    /**
     * 账号搜索
     *
     * @param query
     * @param size
     * @return
     */
    @GetMapping("/suggest")
    public APIResponseBean<List<UserBaseResponseInfoVO>> suggest(
            @RequestParam(value = "q", required = false) String query,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {

        return APIResponseBeanUtil.success(null);
    }

    /**
     * 创建机器人
     *
     * @return
     */
    @PostMapping("/createRobot")
    public APIResponseBean createRobot(@RequestBody CreateRobotRequestVO requestPram) {
        String userId = SessionHelper.getCurrentUserId();
        requestPram.setCreateUserId(userId);
        accountManagementService.createRobot(requestPram);
        return APIResponseBeanUtil.success("机器人创建成功");
    }

    /**
     * 创建公众号
     * 默认只有管理员可以创建公众号
     *
     * @return
     */
    @PostMapping("/createPublicAccount")
    public APIResponseBean createPublicAccount(@RequestBody CreateAccountRequestVO requestPram) {
        String userId = SessionHelper.getCurrentUserId();
        requestPram.setCreateUserId(userId);
        accountManagementService.createPublicAccount(requestPram);
        return APIResponseBeanUtil.success("公众号创建成功");
    }

    /**
     * 创建服务号
     * 默认只有管理员可以创建服务号
     *
     * @return
     */
    @PostMapping("/createServerAccount")
    public APIResponseBean createServerAccount(@RequestBody CreateAccountRequestVO requestPram) {
        String userId = SessionHelper.getCurrentUserId();
        requestPram.setCreateUserId(userId);
        accountManagementService.createServerAccount(requestPram);
        return APIResponseBeanUtil.success("公众号创建成功");
    }
}
